﻿@{
    Ajax.Dna().Widget()
        .UserPreferences(@<text>
    <div style="padding: 5px 0px;">
        <div>
            height</div>
        <div>@Ajax.Dna().TextBox(item.GenerateFieldID("height")).NumericOnly().Value(item.GetInt("height")).GetHtml()</div>
    </div>
    <div style="padding: 5px 0px;">
        <div>
            content String</div>
        <div>@Ajax.Dna().TextBox(item.GenerateFieldID("contentString")).Value(item.GetString("contentString")).GetHtml()</div>
    </div>
    <div style="padding: 5px 0px;">
        <div>
            zoom</div>
        <div>@Ajax.Dna().TextBox(item.GenerateFieldID("zoom")).NumericOnly().Value(item.GetInt("zoom").ToString()).GetHtml()</div>
    </div>
    </text>)
        .Content(@<text>
    @{
        var userProfile = UserProfile.GetProfile(WebSiteContext.Current.Web.Owner);
        string addr = userProfile.Address + " " + userProfile.City + " " + userProfile.State + " " + userProfile.Country;
    }
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
    <script type="text/javascript" src="http://code.google.com/apis/gears/gears_init.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#map_canvas").height(@(item.GetInt("height")));
        window.initialLocation={};
        window.siberia = new google.maps.LatLng(60, 105);
        window.newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
        window.browserSupportFlag = new Boolean();
        window.map={};
        window.infowindow = new google.maps.InfoWindow();
            initialize();
        }) 

        function initialize() {
            var myOptions = {
                zoom: @(item.GetInt("zoom")),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

            map = new google.maps.Map(document.getElementById("@item.GenerateFieldID("map_canvas")"), myOptions);
            var _addr = "@(addr)";

            if (_addr) {
                var geocoder = new google.maps.Geocoder();
                geocoder.geocode({ "address": _addr }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        showGeolocation(results[0].geometry.location, _addr);
                    } else {
                        alert("Geocode was not successful for the following reason: " + status);
                    }
                });
            }
            else {
                showGeolocation(newyork);
            }
        }

        function showGeolocation(_location, _addr) {
            var _contentStr ="@item.GetString("contentString")";
            map.setCenter(_location);
            infowindow.setPosition(_location);
            infowindow.open(map);
            var marker = new google.maps.Marker({
                position: _location,
                map: map,
                title: _addr ? _addr : _contentStr
            });
        }

        function handleNoGeolocation(errorFlag) {
            if (errorFlag == true) {
                initialLocation = newyork;
                contentString = "Error: The Geolocation service failed.";
            } else {
                initialLocation = siberia;
                contentString = "Error: Your browser doesn't support geolocation. Are you in Siberia?";
            }
            map.setCenter(initialLocation);
            infowindow.setContent(contentString);
            infowindow.setPosition(initialLocation);
            infowindow.open(map);
        }

    </script>
    <div id="@item.GenerateFieldID("map_canvas")" style="height:@(item.GetInt("height"))px;">
    </div>
    </text>)
        .Render();
}
